<template>
    <div class="app-container">
        <!-- 搜索 -->
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
            <el-form-item label="账户类型" prop="accountType">
                <el-select v-model="queryParams.accountType" placeholder="请选择账户类型">
                    <el-option v-for="dict in dict.type.sys_price_type" :key="dict.label" :label="dict.label"
                        :value="dict.value"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="账户号" prop="accountNumber">
                <el-input v-model="queryParams.accountNumber" placeholder="请输入账户号" clearable >
                        </el-input>
            </el-form-item>
            <input hidden />
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>
        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" plain icon="el-icon-plus" size="mini"
                    @click="handleAddProducts" v-hasPermi="['pay:account:add']">添加</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple"
                    @click="handleDelete" v-hasPermi="['pay:account:remove']">删除</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getDataList"></right-toolbar>
        </el-row>
        <!-- 表格 -->
        <el-table v-loading="loading" :data="dataList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="ID" prop="id" width="60" />
            <el-table-column label="账户名字" prop="accountTypeName" width="100" />
            <el-table-column label="账户号" prop="accountNumber" />
            <el-table-column label="操作" align="center" width="200" class-name="small-padding fixed-width" fixed="right">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                        v-hasPermi="['pay:account:edit']">修改</el-button>
                    <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                        v-hasPermi="['pay:account:remove']">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- 分页 -->
        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize" @pagination="getDataList" />
        <!-- 添加或编辑弹框 -->
        <el-dialog :title="addDialogTitle" :visible.sync="addDialogVisible" width="400px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" size="medium" label-width="100px">
                <el-row>
                    <el-form-item label="账户名字" prop="accountTypeName">
                        <el-select v-model="form.accountTypeName" value-key="label" placeholder="请选择账户类型"  @change="changeType">
                            <el-option v-for="dict in dict.type.sys_price_type" :key="dict.label" :label="dict.label"
                                :value="dict"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="账户号" prop="accountNumber">
                        <el-input v-model="form.accountNumber" placeholder="请输入账户号" clearable >
                        </el-input>
                    </el-form-item>
                </el-row>
            </el-form>
            <div slot="footer">
                <el-button @click="cancel">取消</el-button>
                <el-button type="primary" @click="submitForm">确定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
// 快递平台管理
import { getList, getInfo, addList, editList, removeList } from '@/api/payment'
export default {
    name: 'CustomerList',
    dicts: ['sys_price_type', 'self_order_payment_type'],
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                accountNumber: undefined, // 平台名字
                accountTypeName: undefined, // 平台编号
            },
            // 数据列表
            dataList: [],
            // 添加弹框标题
            addDialogTitle: '',
            // 添加弹框是否显示
            addDialogVisible: false,
            form: {},
            // 添加弹框表单校验
            rules: {
                accountTypeName: [{ required: true, message: '请选择账户类型', trigger: 'change' }],
                accountNumber: [{ required: true, message: '请输入账户号', trigger: 'blur' }],

            },
            isStatus: false,

        }
    },
    created() {
        this.getDataList()
    },
    methods: {
        changeType(e){
            console.log(e);
            this.form.accountType = e.value
            this.form.accountTypeName = e.label
        },
        /** 查询列表 */
        getDataList() {
            this.loading = true
            getList(this.queryParams).then((response) => {
                this.dataList = response.rows
                this.total = response.total
                this.loading = false
            })
        },
        // 取消按钮
        cancel() {
            this.addDialogVisible = false;
        },
        reset() {
            this.form = {};
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1
            this.getDataList()
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm('queryForm')
            this.handleQuery()
        },
        /** 多选框选中数据 */
        handleSelectionChange(selection) {
            this.ids = selection.map((item) => item.id)
            this.single = selection.length != 1
            this.multiple = !selection.length
        },
        handleAddProducts() {
            this.reset();
            this.isStatus = false
            this.addDialogTitle = '添加收款账号'
            this.addDialogVisible = true
        },
        handleUpdate(row) {
            this.reset();
            getInfo({ id: row.id }).then((response) => {
                this.form = response.data;
                this.isStatus = true;
                this.addDialogVisible = true;
                this.addDialogTitle = "修改收款账号";
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            let userIds = row.id || this.ids;
            if (typeof userIds == "number") {
                userIds = [userIds];
            }
            this.$modal
                .confirm('是否确认删除ID为"' + userIds + '"的数据项？')
                .then(function () {
                    return removeList({ ids: userIds });
                })
                .then(() => {
                    this.getDataList();
                    this.$modal.msgSuccess("删除成功");
                })
                .catch(() => { });
        },
        /** 提交按钮 */
        submitForm: function () {
            console.log(this.form);
            this.$refs["form"].validate((valid) => {
                if (valid) {
                    if (this.isStatus) {
                        editList(this.form).then((response) => {
                            this.$modal.msgSuccess("修改成功");
                            this.cancel()
                            this.getDataList();
                        });
                    } else {
                        addList(this.form).then((response) => {
                            this.$modal.msgSuccess("新增成功");
                            this.cancel()
                            this.getDataList();
                        });
                    }
                }
            });
        },
    },
}
</script>

<style lang="scss" scoped>

</style>